package com.zxx.pattern.flyweight.fly3;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zxx on 2021/7/19
 */
public class TicketFactory {

    //每次都会创建一个对象
    public static Ticket getTicket(String from, String to) {
        return new TrainTicket(from, to);
    }

    private static Map<String, Ticket> ticketMap = new ConcurrentHashMap<>();

    public static Ticket getTicket2(String from, String to) {
        String key = from + "-" + to;
        if (ticketMap.containsKey(key)) {
            return ticketMap.get(key);
        } else {
            Ticket ticket = new TrainTicket(from, to);
            ticketMap.put(key, ticket);
            return ticket;
        }
    }

    public static int getTicketSize() {
        return ticketMap.size();
    }
}
